package com.supertange.miniapp.wepet.modules.wepet.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supertange.miniapp.wepet.common.controller.BaseController;
import com.supertange.miniapp.wepet.common.service.RedisService;
import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import com.supertange.miniapp.wepet.modules.wepet.entity.CommentsEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersReport;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.*;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户相关业务接口", tags = "用户相关业务controller")
public class UserController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "用户上传头像接口", notes = "用户上传头像的接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/uploadFace")
    public AjaxResult uploadFace(String userId,
                                 @RequestParam MultipartFile file) throws Exception {
        if (StringUtils.isBlank(userId)) return AjaxResult.errorMsg("用户id或头像链接不能为空...");
//        String headImg = FileUploadUtil.upload(file, uploadRootPath, "/faceImg");
        String headImg = qnImageUpload(file);
        UsersInfoVO users = new UsersInfoVO();
        users.setId(userId);
        users.setFaceImage(headImg);
        Assert.isZero(userService.updateUserInfo(users),"更新失败");;
        return AjaxResult.ok(headImg);
    }

    @ApiOperation(value = "查询用户个人中心信息", notes = "查询用户个人中心信息的接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true,
            dataType = "String", paramType = "query")
    @PostMapping("/query")
    public AjaxResult query(String userId, String fanId) {

        if (StringUtils.isBlank(userId)) {
            return AjaxResult.errorMsg("用户id不能为空...");
        }
        //查询用户详情
        UsersEntity userInfo = userService.queryUserInfo(userId);
        UsersVO userVO = new UsersVO();
        BeanUtils.copyProperties(userInfo, userVO);
        userVO.setFollow(userService.queryIfFollow(userId, fanId));
        return AjaxResult.ok(userVO);
    }


    /**
     * 查询自己的粉丝
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping("/queryMyFans")
    public AjaxResult queryMyFans(@RequestParam String userId,
                                  Integer page,
                                  Integer pageSize) {
        if (page == null) page = PAGE;
        if (pageSize == null) pageSize = PAGE_SIZE;
        Page<CommentsEntity> pageInfo = new Page<>(page, pageSize);
        List<UsersFansVO> fansVOList = userService.queryMyFans(userId, pageInfo);
        for (int i=0;i<fansVOList.size();i++){
            String fanId=fansVOList.get(i).getFanId();
            fansVOList.get(i).setIsFollow(String.valueOf(userService.queryIfFollow(fanId,userId)));
        }
        return AjaxResult.ok(fansVOList);
    }

    /**
     * 查询自己的关注用户
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping("/queryMyFollow")
    public AjaxResult queryMyFollow(@RequestParam String userId,
                                  Integer page,
                                  Integer pageSize) {
        if (page == null) page = PAGE;
        if (pageSize == null) pageSize = PAGE_SIZE;
        Page<CommentsEntity> pageInfo = new Page<>(page, pageSize);
        List<UsersFansVO> fansVOList = userService.queryMyFollow(userId, pageInfo);
        for (int i=0;i<fansVOList.size();i++){
            String fanId=fansVOList.get(i).getUserId();
            fansVOList.get(i).setIsFollow(String.valueOf(userService.queryIfFollow(userId,fanId)));
        }
        return AjaxResult.ok(fansVOList);
    }

    /**
     * 存储用户信息
     *
     * @param usersInfoVO
     * @return
     */
    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    public AjaxResult updateUserInfo(UsersInfoVO usersInfoVO) {
        Assert.isZero(userService.updateUserInfo(usersInfoVO),"更新失败");
        return AjaxResult.ok("更新信息成功");
    }

    /**
     * 用户名查重controller
     *
     * @param nickname 昵称
     * @return
     */
    @PostMapping("/isNicknameRecur")
    public AjaxResult IsExistNickname(@RequestParam String nickname) {
        Assert.isFalse(userService.queryNicknameIsExist(nickname), "已存在该昵称");
        return AjaxResult.ok("可使用该昵称");
    }

    /**
     * 查询发布者的信息
     *
     * @param loginUserId
     * @param videoId
     * @param publishUserId
     * @return
     */
    @PostMapping("/queryPublisher")
    @ApiOperation(value = "查询发布者信息", notes = "查询发者的信息接口")
    public AjaxResult queryPublisher(String loginUserId,
                                     String videoId,
                                     String publishUserId) {

        if (StringUtils.isBlank(publishUserId)) {
            return AjaxResult.errorMsg("");
        }

        // 1. 查询视频发布者的信息
        UsersEntity userInfo = userService.queryUserInfo(publishUserId);
        UsersVO publisher = new UsersVO();
        publisher.setFollow(userService.queryIfFollow(publishUserId, loginUserId));
        BeanUtils.copyProperties(userInfo, publisher);

        // 2. 查询当前登录者和视频的点赞关系
        boolean userLikeVideo = userService.isUserLikeVideo(loginUserId, videoId);

        PublisherVideoVO bean = new PublisherVideoVO();
        bean.setPublisher(publisher);
        bean.setUserLikeVideo(userLikeVideo);

        return AjaxResult.ok(bean);
    }

    @PostMapping("/beyourfans")
    @ApiOperation(value = "关注用户接口", notes = "关注用户接口")
    public AjaxResult beyourfans(String userId, String fanId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
            return AjaxResult.errorMsg("");
        }

        userService.saveUserFanRelation(userId, fanId);

        return AjaxResult.ok("关注成功...");
    }

    @PostMapping("/dontbeyourfans")
    @ApiOperation(value = "取消关注用户接口", notes = "取消关注用户接口")
    public AjaxResult dontbeyourfans(String userId, String fanId) throws Exception {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
            return AjaxResult.errorMsg("");
        }

        userService.deleteUserFanRelation(userId, fanId);

        return AjaxResult.ok("取消关注成功...");
    }

    @PostMapping("/reportUser")
    @ApiOperation(value = "举报用户", notes = "举报用户接口", httpMethod = "POST")
    public AjaxResult reportUser(@RequestBody UsersReport usersReport) {

        // 保存举报信息
        userService.reportUser(usersReport);

        return AjaxResult.errorMsg("举报成功...有你平台变得更美好...");
    }

    @GetMapping("/getMyComments")
    @ApiOperation(value = "获取自己的所有评论", tags = "获取评论接口", httpMethod = "POST")
    public AjaxResult getMyComments(@RequestParam String userId,
                                    Integer page,
                                    Integer pageSize) {
        if (page == null) page = PAGE;
        if (pageSize == null) pageSize = PAGE_SIZE;
        Page<CommentsEntity> pageInfo = new Page<>(page, pageSize);
        List<CommentsVO> iPage = userService.getAllComments(userId, pageInfo);
        return AjaxResult.ok(iPage);
    }

    @PostMapping("/praiseComment")
    @ApiOperation(value = "举报用户", notes = "举报用户接口", httpMethod = "POST")
    public AjaxResult praiseComment(@RequestParam String commentId,
                                    @RequestParam String userId) throws Exception {


        return AjaxResult.ok();
    }

}
